package cn.gene.timeyan.ums.webapi.ums.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.ums.dto.SignInDTO;
import cn.gene.timeyan.pojo.ums.model.SignIn;
import cn.gene.timeyan.ums.service.ISignInService;
import cn.gene.timeyan.ums.webapi.ums.mapper.SignInMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户签到    服务实现类*
 *
 * @author LWG
 * @date 2022/7/22 17:33
 * @since 1.0.0@gene.cn
 */
@Slf4j
@Service
public class SignInServiceImpl implements ISignInService {
    @Autowired
    SignInMapper signInMapper;
    @Override
    public void SignIn(SignInDTO signInDTO) {
        //查询是否有记录
        SignIn now = signInMapper.selectByUserId(signInDTO.getUserId());
        SignIn today = signInMapper.selectByToday(signInDTO.getUserId());
        SignIn yesterday = signInMapper.selectByYesterday(signInDTO.getUserId());
        if(now==null){
            //添加积分
            Integer row = signInMapper.addScore(signInDTO.getUserId(),10);
            Integer info = signInMapper.addUserInfo(signInDTO.getUserId(),10);
            if(row!=1||info!=1){
                throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,row+"积分服务器忙，稍后再试");
            }
            //第一次
            log.info("111111111111111111111");
            SignIn sign = new SignIn();
            BeanUtils.copyProperties(signInDTO,sign);
            sign.setDuration(1);
            sign.setMaxDuration(1);
            Integer rows = signInMapper.insertSign(sign);
        }else if(yesterday==null&&today==null){
            //添加积分
            Integer row = signInMapper.addScore(signInDTO.getUserId(),2);
            Integer info = signInMapper.addUserInfo(signInDTO.getUserId(),10);
            if(row!=1||info!=1){
                throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,row+"积分服务器忙，稍后再试");
            }
            //断签
            log.info("22222222222222222222222");
            signInMapper.updateReset(signInDTO.getUserId());
        }else if(yesterday!=null&&today==null){
            //添加积分
            Integer row = signInMapper.addScore(signInDTO.getUserId(),2);
            Integer info = signInMapper.addUserInfo(signInDTO.getUserId(),10);
            if(row!=1||info!=1){
                throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,row+"积分服务器忙，稍后再试");
            }
            //连签
            log.info("33333333333333333333333333333333333333333");
            signInMapper.updateDuration(signInDTO.getUserId());
            signInMapper.updateMaxDuration(signInDTO.getUserId());

        }else{
            //已经签到
            log.info("444444444444444444444444444");
            throw new TimeyanServiceException(ResponseCode.ERR_SIGNIN_WAS,"你已签到");
        }

    }
}
